﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForm_Mes.Properties;

namespace WinForm_Mes.WorkingManagement
{
    public class HttpClientHelper
    {
        public async static Task<string> RequestUrl(RequestType method,string url,string data="")
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(Settings.Default.BaseUrl);
                HttpResponseMessage response = null;
                HttpContent httpContent = null;
                switch (method)
                {
                    case RequestType.get:
                        response = await client.GetAsync(url);
                        break;
                    case RequestType.post:
                        httpContent = new StringContent(data,Encoding.UTF8,"application/json");
                        response = await client.PostAsync(url, httpContent);
                        break;
                    case RequestType.delete:
                        response = await client.DeleteAsync(url);
                        break;
                    case RequestType.put:
                        httpContent = new StringContent(data, Encoding.UTF8, "application/json");
                        response = await client.PutAsync(url, httpContent);
                        break;
                    default:
                        break;
                }
                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadAsStringAsync();
                }
                else
                {
                    XtraMessageBox.Show($"请求失败：{response.RequestMessage}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                    return string.Empty;
            }
            
        }
    }
}
